/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ve.usb.coord.colision;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ve.usb.coord.colision.hibernate.Coordinacion;
import ve.usb.coord.colision.hibernate.Departamento;
import ve.usb.coord.colision.hibernate.HibernateUtil;
import ve.usb.coord.colision.hibernate.Oferta;
import ve.usb.coord.colision.hibernate.OfertaEnviada;

/**
 *
 * @author Fernando
 */
public class AccionesOfertaEnviar extends org.apache.struts.action.Action {
    
    /* forward name="success" path="" */
    private final static String SUCCESS = "success";
    
    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm  form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        
        Session s = HibernateUtil.getSessionFactory().getCurrentSession();
        Transaction tr = s.beginTransaction();
        try {
             

            FOfertaEnviar fFOfertaEnviar = (FOfertaEnviar)form;
            
            OfertaEnviada ofertaEnviada = new OfertaEnviada();
            
            Coordinacion coord = (Coordinacion)s.load(Coordinacion.class, new Long(fFOfertaEnviar.getIdCoordinacion())); 
            ofertaEnviada.setCoordinacion(coord);
            Departamento depart = (Departamento)s.load(Departamento.class, new Long(fFOfertaEnviar.getIdDepartamento())); 
            ofertaEnviada.setDepartamento(depart);
            Oferta oferta = (Oferta)s.load(Oferta.class, new Long(fFOfertaEnviar.getIdOferta())); 
            ofertaEnviada.setOferta(oferta);
            Date fecha = new Date();
            ofertaEnviada.setFechaEnvio(fecha);
            Byte aprobado = new Byte("0");
            ofertaEnviada.setAprobado(aprobado.byteValue());
            s.save(ofertaEnviada);
            s.flush();
            tr.commit();

        } catch (Exception ex) {
            tr.rollback();
            throw ex;
        }
        return mapping.findForward(SUCCESS);
    }
    
}